using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PocketAbapa
{
	public partial class Field : UserControl
	{
		public Field()
		{
			InitializeComponent();

			Players = new PlayersList(2);
			Players.CurrentPlayerChanged += new EventHandler(CurrentPlayerChanged);
			
			Services = new FieldServices(this);

			buttons = new HouseList(this);
			Buttons.Enable();
		}

		#region Events

		public event EventHandler ScoreChanged;
		protected void RaiseScoreChanged()
		{
			if (ScoreChanged != null)
			{
				ScoreChanged(this, null);
			}
		}

		public event EventHandler CurrentPlayerWon;
		protected void RaiseCurrentPlayerWon()
		{
			if (CurrentPlayerWon != null)
			{
				CurrentPlayerWon(this, null);
			}
		}

		#endregion

		#region Reset

		public void Reset()
		{
			Players.Reset();
			Buttons.Reset();
		}

		#endregion

		#region Parameters

		public int DefaultNumberOfBalls
		{
			get
			{
				return 4;
			}
		}

		private const int size = 6;
		public int FieldSize
		{
			get
			{
				return size;
			}
		}

		public int DefaultTotalNumberOfBalls
		{
			get
			{
				return DefaultNumberOfBalls * 2 * FieldSize;
			}
		}

		#endregion

		#region Buttons

		private HouseList buttons;
		public HouseList Buttons
		{
			get
			{
				return buttons;
			}
		}

		public House this[int index]
		{
			get
			{
				return this.Buttons[index];
			}
			set
			{
				this.Buttons[index] = value;
			}
		}

		void CurrentPlayerChanged(object sender, EventArgs e)
		{
			Buttons.Enable();
		}

		#endregion

		#region Services

		private FieldServices Services;

		#endregion

		#region Players

		private PlayersList mPlayers;
		public PlayersList Players
		{
			get
			{
				return mPlayers;
			}
			set
			{
				mPlayers = value;
			}
		}

		public bool DidCurrentPlayerWin()
		{
			return DidPlayerWin(this.Players.CurrentPlayerIndex);
		}

		public bool DidPlayerWin(int playerNum)
		{
			return this.Players[playerNum].Score > this.DefaultTotalNumberOfBalls / this.Players.Count;
		}

		#endregion

		#region Move

		public void HouseClicked(House house)
		{
			int anyBallsCollected = Services.RedistributeBalls(house.Index);
			if (anyBallsCollected > 0)
			{
				RaiseScoreChanged();
			}
			if (DidCurrentPlayerWin())
			{
				RaiseCurrentPlayerWon();
			}
			else
			{
				this.Players.SetNextPlayer();
			}
		}

		#endregion

		#region Layout

		private void Layout()
		{
			// Don't layout if the buttons don't exist yet
			if (Buttons == null)
			{
				return;
			}

			int w = this.Width;
			int h = this.Height;
			int buttonHeight = h / 2;

			int x1 = 0;
			int x2 = 0;

			this.SuspendLayout();

			for (int i = 0; i < size; i++)
			{
				House upper = Buttons[size - i - 1];
				House lower = Buttons[size + i];

				x1 = w * i / size;
				x2 = w * (i + 1) / size;
				upper.Bounds = new Rectangle(
					x1,
					0,
					x2 - x1,
					buttonHeight);
				lower.Bounds = new Rectangle(
					x1,
					buttonHeight,
					x2 - x1,
					h - buttonHeight);
			}

			this.ResumeLayout();
		}

		private void Field_Resize(object sender, EventArgs e)
		{
			Layout();
		}

		#endregion
	}
}
